#include"Util.hpp"
#include<cstdio>
#include<vector>
#define INIT(v) do{ \
        v.push_back(printlog);\
        v.push_back(download);\
        v.push_back(executeSql);\
    }while(0)

#define EXEX_OTHER(v) do{ \
    for(auto const &  cb:v) cb();\ 
    }while(0)


int main()
{
    std::vector<func_t> callback;
    INIT(callback);
    setNoBlock(0);//设置为非阻塞
    while (true)
    {
        printf(">>>>> ");
        fflush(stdout);
        sleep(1);
        // 从0号文件描述符(键盘)里面进行读取！
        char buffer[1024];
        ssize_t s = read(0, buffer,sizeof(buffer)-1);
        if(s > 0)
        {
            buffer[s] = 0;
            std::cout <<"echo: " << buffer ;
        }
        else if(s == 0)//读取到文件尾
        {
            std::cout <<"read end" <<std::endl;
            break;
        } 
        else //为负数
        {
            // std::cout << "EAGAIN " << EAGAIN << std::endl;
            // std::cout << "EWOULDBLOCK " <<EWOULDBLOCK<<std::endl;
            if(errno == EAGAIN || errno == EWOULDBLOCK)
            {
                std::cout << "no reeor!no data!" <<std::endl;
                EXEX_OTHER(callback);
            }
            else if(errno == EINTR)
            {
                continue;//继续重新读取
            }//除了这两种剩下的全是报错！
            else
            {

                std::cout << "result: " << s << " " << "errno: " << errno << " " << strerror(errno) << std::endl;
            }
            
        }

    }
    return 0;
}